<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :title="displayModuleTitle"
    @ok="onOkButtonClick"
  >
    <BasicForm @register="registerForm"></BasicForm>
  </BasicModal>
</template>
<script lang="ts">
  import { computed, defineComponent, ref, unref } from 'vue';
  import { BasicModal, useModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { displayTitle } from '/@/utils/QcUnifyUtil';
  import { OperationTypeEnum } from '/@/enums/appEnum';
  import { QcUnifyTransToChildProps } from '/#/QcUnify';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { addWarehouseInventorySchema } from './warehouseInventory.data';
  import { addWareHouseInventory } from '/@/api/ehs/warehouseInventory';
  const { createMessage } = useMessage();
  export default defineComponent({
    components: { BasicModal, BasicForm },
    name: 'AddWareHouseInventoryModal',
    emits: ['success'],
    props: ['optypeOptions'],
    setup(props, { emit }) {
      const warehouseid = ref('');
      const operateType = ref(OperationTypeEnum.UN_KNOWN);
      const [registerForm, { setFieldsValue, resetFields, validate, updateSchema }] = useForm({
        labelWidth: 90,
        schemas: addWarehouseInventorySchema,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      const qcUnifyProps = ref<QcUnifyTransToChildProps>();
      const [register, { closeModal }] = useModalInner((data) => {
        resetFields();
        if (data != undefined && data != null) {
          let unrefData = unref(data);
          qcUnifyProps.value = unrefData;
          warehouseid.value = unrefData.warehouseid;
          //接收父组件传递的数据
          if (unrefData?.operateType) {
            //判断是否传入op属性
            operateType.value = unrefData.operateType;
          }
          console.log(props.optypeOptions, 'optypeOptions');
          updateSchema([
            {
              field: 'optype',
              componentProps: {
                options: props.optypeOptions,
              },
            },
          ]);
        }
      });
      async function onOkButtonClick() {
        const values = await validate();
        if (values) {
          addWareHouseInventory({
            ...values,
            warehouseid: warehouseid.value,
            attachment: JSON.stringify(values.attachment),
          }).then((res) => {
            if (!res) {
              createMessage.success('添加成功');
              emit('success');
              closeModal();
            } else {
              createMessage.error('添加失败, 请稍后重试');
            }
          });
        }
      }

      let displayModuleTitle = computed(() => {
        return displayTitle(qcUnifyProps.value);
      });
      return {
        register,
        registerForm,
        closeModal,
        displayModuleTitle,
        onOkButtonClick,
      };
    },
  });
</script>
